package com.atguigu.tingshu.order.consumer;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
@Slf4j
@Component
public class OrderCancelConsumer {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private OrderInfoService orderInfoService;
    @PostConstruct
    public void cancelConsumer(){
        Executors.newSingleThreadExecutor().submit(()->{
            //功能未完善 需要微信支付或余额支付后 用户未支付才需要进行幂等性校验 避免修改订单事失误
           while (true){
               try {
                   RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(MqConst.QUEUE_ORDER_CANCEL);
                   Object take = blockingDeque.take();
                   System.out.println(take);
                   log.info("成功修改订单状态为取消，订单为："+take);

                   orderInfoService.updateCancelOrder(take.toString());
               } catch (InterruptedException e) {
                   e.printStackTrace();
                   log.error("修改订单状态失败，"+e.getMessage());
                   throw new RuntimeException(e);
               }

           }
        });
    }
}
